package com.example.easycharge.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.easycharge.notRepEntity.ChargeRequest;
import com.example.easycharge.notRepEntity.NewCapacity;
import com.example.easycharge.repository.VehicleRepository;
import com.example.easycharge.service.ChargeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Tag(name = "充电接口组")
@RestController
@RequestMapping("charge")
public class ChargeController {
    @Autowired
    private ChargeService chargeService;

    @Operation(description = "申请充电")
    @PostMapping("")
    public JSONObject startCharge(HttpServletRequest httpServletRequest, @RequestBody ChargeRequest chargeRequest) {
        long customerId = (long) httpServletRequest.getAttribute("customerId");
        return chargeService.beforeRequest(customerId, chargeRequest);
    }

    @Operation(description = "取消充电")
    @PutMapping("cancel/{vehicleId}")
    public JSONObject cancelCharge(@PathVariable("vehicleId") long vehicleId) {
        return chargeService.cancelCharge(vehicleId);
    }

    @Operation(description = "插入充电桩的接口，不合规的操作会返回错误码（比如非预约车辆、充电桩状态不符、无此充电桩等）")
    @PutMapping("plugIn/{vehicleId}")
    public JSONObject plugInPile(@PathVariable("vehicleId") long vehicleId) {
        return chargeService.plugInPile(vehicleId);
    }

    @Operation(description = "拔出充电头的接口，操作不当的话会返回错误码（你不能还没插进去就拔出来了）")
    @PutMapping("unplug/{vehicleId}")
    public JSONObject unplugPile(@PathVariable("vehicleId") long vehicleId) {
        return chargeService.unplugPile(vehicleId);
    }

    @Operation(description = "模拟车辆驶入的接口，由于考虑到车辆占位的影响，因此在大多数情况下都会驶入成功（虽然可能占了别人的车位），只在该车位有车的情况下没法驶入（废话，会返回错误码）")
    @PutMapping("driveIn/{vehicleId}")
    public JSONObject driveIn(@PathVariable("vehicleId") long vehicleId) {
        // 因为驶入接口是模拟红外感应和视频识别的，所以不做封装直接返回
        return chargeService.driveIn(vehicleId);
    }

    @Operation(description = "车辆驶离接口，也有一些地方需要注意，如果车位上停的不是你的车的话就会返回错误码。")
    @PutMapping("driveAway/{vehicleId}")
    public JSONObject driveAway(@PathVariable("vehicleId") long vehicleId) {
        // TODO: 2022-05-30 超时停车费
        // 驶离接口不同于驶入接口，不在车位上的驶离毫无意义也不可能发生，因此还是捕获异常
        return chargeService.driveAway(vehicleId);

    }

    @Operation(description = "修改充电模式接口")
    @PutMapping("pattern/{vehicleId}/{newPattern}")
    public JSONObject changePattern(@PathVariable("newPattern") boolean isFast,
                                    @PathVariable("vehicleId") long vehicleId) {
        return chargeService.resetPatten(vehicleId, isFast);
    }

    @Operation(description = "修改充电电量")
    @PutMapping("/capacity/{vehicleId}")
    public JSONObject changeCapacity(@RequestBody NewCapacity newCapacity, @PathVariable long vehicleId) {
        return chargeService.resetCapacity(vehicleId, newCapacity);
    }

}
